package com.ash.android_adk_demo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ash.base.bean.Cure
import com.ash.base.bean.Schemes

class ParentAdapter(
    private val data: MutableList<Cure>,
    private var mOnItemClickLitener: OnItemClickLitener? = null
) : RecyclerView.Adapter<ParentAdapter.ParentViewHolder>() {



    //设置回调接口
    public interface OnItemClickLitener{
        fun onItemClick( view:View, scheme:Schemes)
    }


    public fun setOnItemClickLitener( mOnItemClickLitener:OnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParentViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_parent, parent, false)
        return ParentViewHolder(view)
    }

    override fun onBindViewHolder(holder: ParentViewHolder, position: Int) {
        holder.bind(data[position])
    }

    override fun getItemCount() = data.size

    inner class ParentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvName: TextView = itemView.findViewById(R.id.tvName)
        private val rvSubItems: RecyclerView = itemView.findViewById(R.id.rvSubItems)

        fun bind(item: Cure) {
            tvName.text =  item.typeName
            // 设置子列表的适配器
            rvSubItems.layoutManager = LinearLayoutManager(
                itemView.context,
                LinearLayoutManager.HORIZONTAL,
                false
            )
            var subAdapter = SubAdapter(item.schemes,mOnItemClickLitener)
            rvSubItems.adapter = subAdapter
        }
    }
}